package com.zhang.aspect;

import com.zhang.util.RequestHolder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;

// 切面类
@Aspect
@Component
public class MyAspect {
    // 定义切点，匹配使用 MyAnnotation 注解的方法
    @Pointcut("@annotation(com.zhang.anno.MyAnnotation)")
    public void annotationPointcut() {}

    // 在切点方法执行前执行此方法
    @Before("annotationPointcut()")
    public void beforeTask(JoinPoint joinPoint) {
        // 获取 request 信息
        HttpServletRequest request = RequestHolder.getRequest();
        if (request != null) {
            System.out.println("反射中的: "+request.getRequestURI());
        } else {
            System.out.println("Request is null.");
        }
    }
}    